package com.zxkj.login.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.launcher.ARouter;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.entity.ACache;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.MobileTool;
import com.czy.basecommon.utils.tools.SPUtils;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.zcw.togglebutton.ToggleButton;
import com.zxkj.login.BR;
import com.zxkj.login.R;
import com.zxkj.login.databinding.ActivityLoginBinding;
import com.zxkj.login.viewmodel.activity.LoginViewModel;

import java.util.List;

public class LoginActivity extends BaseVmDataBindingActivity<ActivityLoginBinding,LoginViewModel> {


   private  boolean  isOffline;


    @Override
    protected void initUI() {

        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                // 支持请求6.0悬浮窗权限8.0请求安装权限
                //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES)
                // 不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll){


//                         //加载fragment
//                          initFragment();
//                          addFragment();


                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {

                    }
                });
        binding.btnStatus.setToggleOn();
        binding.tbOff.setVisibility(View.GONE);
        binding.tbOn.setVisibility(View.VISIBLE);
        binding.btnStatus.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    isOffline=false;

                    binding.tbOff.setVisibility(View.GONE);
                    binding.tbOn.setVisibility(View.VISIBLE);
                }else{

                    isOffline=true;
                    binding.tbOn.setVisibility(View.GONE);
                    binding.tbOff.setVisibility(View.VISIBLE);
                }
            }
        });


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.data.observe(this, new Observer<StateData<String>>() {
            @Override
            public void onChanged(StateData<String> stringStateData) {
               if ( stringStateData.getStatus()==StateData.DataStatus.SUCCESS){
                   Log.i("SUCCESS",stringStateData.getData());
               }else{
                   Log.e("Error",stringStateData.getMsg());
               }
            }
        });


    }

    @Override
    protected void setListener() {
       binding.mbGoLogin.setOnClickListener(v->{
//           if (isOffline){
//               SPUtils.put("onLine","2");
//               startActivity(SynActivity.class);
//               ActivityTool.getInstance().popOneActivity(this);
//           }else{
//               SPUtils.put("onLine","1");
////               startActivity(Employee_Information_Activity.class);
//               ARouter.getInstance().build("/Line/LinePatrolActivity").navigation();
//               ActivityTool.getInstance().popOneActivity(this);
//           }

//           ActivityTool.getInstance().popOneActivity(this);
//           SPUtils.put("radom", 1);
//           ActivityTool.getInstance().popOneActivity(LoginActivity.this);
//           ARouter.getInstance().build("/Line/LinePatrolActivity").navigation();
//           viewModel.upData();
       });
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.mLoginViewModel;
    }


}
